package com.xhsj.user.hr.process.overtime.web;

import com.xhsj.user.hr.base.BaseParam;
import com.xhsj.user.hr.process.dto.processDefinition.HandleAuditDto;
import com.xhsj.user.hr.process.hrLeaveProcess.query.CompensatoryLeaveTimeQuery;
import com.xhsj.user.hr.process.overtime.entity.TbHrOvertimeApplyTime;
import com.xhsj.user.hr.process.overtime.query.OverTimeApplyFindAllQuery;
import com.xhsj.user.hr.process.overtime.vo.FindUsageVo;
import com.xhsj.user.hr.system.service.TbHrLeaveUsableLengthService;
import com.xhsj.user.hr.system.vo.OverTimeApplyAuditTaskVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.text.ParseException;
import java.time.LocalDate;
import java.util.*;
import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;

import org.springframework.web.bind.annotation.RestController;


import com.xhsj.user.hr.process.overtime.service.TbHrOvertimeApplyService;
import com.xhsj.user.hr.process.overtime.entity.TbHrOvertimeApply;


/**
 * <p>
 * 加班申请 前端控制器
 * </p>
 *
 * @author suxiaolin
 * @since Oct 16, 2020 2:36:22 PM
 */
@Slf4j
@Api(tags = "加班申请")
@RestController
@RequestMapping("/overtime/tbHrOvertimeApply")
public class TbHrOvertimeApplyController {

	@Autowired
    public TbHrOvertimeApplyService service;

	@Autowired
	private TbHrLeaveUsableLengthService tbHrLeaveUsableLengthService;


	/**
	 * 保存/修改
	 * @author suxiaolin
	 * @date Oct 16, 2020 2:36:22 PM
	 */
	@ApiOperation(value = "加班申请,保存/修改", notes = "加班申请,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
//    @PreAuthorize("hasAuthority('tb:hr:overtime:apply:edit')")
	public Message save(@RequestBody TbHrOvertimeApply data){
    	try {
			data.setOpinionText("提交申请");
    		service.saveData(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,e.getMessage());
		}

	}


	@ApiOperation(value = "重新提交", notes = "重新提交")
	@RequestMapping(value = "/reSubmit", method = RequestMethod.POST)
	public Message reSubmit(@RequestBody TbHrOvertimeApply data) {
		Long processTaskId = data.getProcessTaskId();
		if (processTaskId == null) {
			return Message.error(1,"processTaskId不能为空");
		}
		try {
			data.setOpinionText("重新提交");
			service.reSubmit(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,e.getMessage());
		}
	}


    @ApiOperation(value = "作废申请", notes = "作废申请")
    @RequestMapping(value = "/disable", method = RequestMethod.GET)
	public Message disable(@RequestParam("processTaskId") Long processTaskId, @Param("dataId") Long dataId) {
        try {
            service.disable(processTaskId,dataId);
            return Message.success("成功");
        } catch (Exception e) {
            log.error("保存失败:{}",e);
            return Message.error(1,e.getMessage());
        }

    }

	/**
	 * 查询待审核任务
	 * @author 苏小林
	 * @date 2020/9/4 13:22
	 */
	@ApiOperation(value = "查询待审核任务", notes = "查询待审核任务")
	@RequestMapping(value="/findRunTask",method = RequestMethod.POST)
	public Message findRunTask(@RequestBody BaseParam baseParam) {
		try {
			IPage<OverTimeApplyAuditTaskVo> iPage = service.findRunTask(baseParam);
			return Message.success("成功",iPage.getRecords(),iPage.getTotal(),iPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 查询历史任务
	 * @author 苏小林
	 * @date 2020/9/4 17:43
	 * @param: baseParam
	 * @return: com.xhsj.user.utils.Message
	 */
	@ApiOperation(value = "查询历史任务", notes = "查询历史任务")
	@RequestMapping(value="/findHiTask",method = RequestMethod.POST)
	public Message findHiTask(@RequestBody BaseParam baseParam) {
		try {
			IPage<OverTimeApplyAuditTaskVo> iPage = service.findHiTask(baseParam);
			return Message.success("成功",iPage.getRecords(),iPage.getTotal(),iPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}
	}

	/**
	 * 流程审核
	 * @author 苏小林
	 * @date 2020/9/4 15:51
	 */
	@ApiOperation(value = "流程审核", notes = "流程审核")
	@RequestMapping(value = "/processAudit", method = RequestMethod.POST)
	public Message processAudit(@RequestBody HandleAuditDto handleAuditDto, BindingResult bindingResult) {

		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}

		try {
			service.processAudit(handleAuditDto);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,e.getMessage());
		}

	}

	/**
	 * 通过id删除数据
	 * @author suxiaolin
	 * @date Oct 16, 2020 2:36:22 PM
	 */
	@ApiOperation(value = "加班申请删除", notes = "通过id删除加班申请信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('tb:hr:overtime:apply:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}
	}
	/*@RequestMapping(value = "/test", method = RequestMethod.GET)
	public CompensatoryLeaveTimeQuery test() throws ParseException {
		CompensatoryLeaveTimeQuery overTimeUsableLength = tbHrLeaveUsableLengthService.getOverTimeUsableLength(284L, LocalDate.now());
		return overTimeUsableLength;
	}*/


	/**
	 * 批量删除
	 * @author suxiaolin
	 * @date Oct 16, 2020 2:36:22 PM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:overtime:apply:delete')")
	public Message batchDelete(@RequestBody List<Long> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

    /**
    * 分页查询数据
    * @author suxiaolin
    * @date Oct 16, 2020 2:36:22 PM
    */
	@ApiOperation(value = "分页查询加班申请list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:overtime:myApply:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbHrOvertimeApply> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

	/**
	 * 查询加班使用情况
	 * @author 苏小林
	 * @date 2020/10/23 14:35
	 */
	@RequestMapping(value="/findUsage",method = RequestMethod.POST)
	public Message findUsage(@RequestBody OverTimeApplyFindAllQuery data) {

		try {
			IPage<FindUsageVo> iPage = service.findUsage(data);
			return Message.success("成功",iPage.getRecords(),iPage.getTotal(),iPage.getPages());
		} catch (Exception e) {
			log.error("查询异常:{}",e);
			return Message.error(1,"失败");
		}

	}

	/**
	 * 查询年度用户所有的加班详情 - 审核通过的数据
	 * @author 苏小林
	 * @date 2020/10/26 10:53
	 */
	@RequestMapping(value="/findDetailByUserId",method = RequestMethod.GET)
	public Message findDetailByUserId(@RequestParam("userId") String userId,@RequestParam("year") String year) {

		List<TbHrOvertimeApplyTime> list = service.findDetailByUserId(userId,year);
		return Message.success("成功",list);
	}

    /**
    * 查询所有加班申请数据
    * @author suxiaolin
    * @date Oct 16, 2020 2:36:22 PM
    */
	@ApiOperation(value = "查询加班申请所有数据list", notes = "查询加班申请所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:overtime:allApply:view')")
	public Message findAll(@RequestBody OverTimeApplyFindAllQuery data){
		IPage<TbHrOvertimeApply> selectPage = service.findAllOverTimeApplyList(data);
		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
	}

    /**
    * 通过id获取实体类
    * @author suxiaolin
    * @date Oct 16, 2020 2:36:22 PM
    */
	@ApiOperation(value = "通过id获取加班申请实体类", notes = "通过id获取加班申请实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
	public Message getById(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author suxiaolin
    * @date Oct 16, 2020 2:36:22 PM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
	public Message detail(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

}

